<template>
  <div @click="getCode()">
    微信公众号网页授权
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      code: '',
    };
  },
  created() {
    // this.getCode()
  },
  mounted() {

  },

  methods: {//（以vue为例）
    getCode() { 
      this.code = ''
      // var local = window.location.href // 获取页面url
      var local = 'https://fygo.zjich.cn/ic_heritage_api'
      var appid = 'wx77dec9047c8c94d9'
      this.code = this.getUrlCode().code // 截取code
      if (this.code == null || this.code === '') { // 如果没有code，则去请求
        // window.location.href =
        //   // `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
        //   `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=https%3A%2F%2Ffygo.zjich.cn%2Fic_heritage_api%2Fwz%2Ffygo%2FgetOpenid&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
        //   console.log("window.location.href", window.location.href);
         this.$router.push({ path: "/code1", query: {  } });
         
      } else {
        console.log(this.code,'9999998888888')
        
      }
    },
    getUrlCode() { // 截取url中的code方法
      var url = location.search
      console.log(url,'00')
      this.winUrl = url
      var theRequest = new Object()
      if (url.indexOf("?") != -1) {
        var str = url.substr(1)
        var strs = str.split("&")
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
        }
      }
      console.log(theRequest,'998777')
      return theRequest
    }
  }
}
</script>
<style scoped></style>